import { formDetailApp } from '@/api/modules/form';
import { getVal } from '@/components/DynamicFormRender/config';
import { feildTypeValue, fixedCombinationCode } from '@/common/formConfig';

const columns = ref<any>({});
const formDetailList = ref<any>({});
const fileFeildList = ref<any[]>([]);
const previewVisible = ref(false);
export const getFormInfo = (params: any) => {
  console.log(params);
  return new Promise((resolve, reject) => {
    formDetailApp(params).then((res: any) => {
      formDetailList.value[res.formId] = res;
      columns.value[res.formId] = [];
      res.fieldList.forEach((item: any) => {
        if (item.fieldType === 8) {
          columns.value[res.formId].push({
            title: item.fieldName,
            fieldId: item.fieldId,
            visibleFlag: item.visibleFlag,
            list: [],
          });
          return;
        }
        if (columns.value[res.formId].length === 0) {
          columns.value[res.formId].push({
            title: undefined,
            list: [],
          });
        }
        const temp =
          columns.value[res.formId][columns.value[res.formId].length - 1];

        // 固定组合值处理
        if (item.fieldType === 12) {
          const valueList = getVal(item, feildTypeValue[item.fieldType]);
          valueList.forEach((value: any, index: number) => {
            if (item.fieldName === '专业+PI+职称') {
              fixedCombinationCode.piInfo.forEach((el) => {
                temp.list.push({
                  prop: el.prop + index,
                  label: el.label,
                  fieldId: item.fieldId,
                  visibleFlag: item.visibleFlag,
                });
                formDetailList.value[res.formId][el.prop + index] = getVal(
                  value,
                  el.valueProp
                );
              });
            }
            if (item.fieldName === '申办方信息') {
              fixedCombinationCode.sponsorInfo.forEach((el) => {
                temp.list.push({
                  prop: el.prop + index,
                  label: el.label,
                  fieldId: item.fieldId,
                  visibleFlag: item.visibleFlag,
                });
                formDetailList.value[res.formId][el.prop + index] = getVal(
                  value,
                  el.valueProp
                );
              });
            }
            if (item.fieldName === 'CRO信息') {
              fixedCombinationCode.croInfo.forEach((el) => {
                temp.list.push({
                  prop: el.prop + index,
                  label: el.label,
                  fieldId: item.fieldId,
                  visibleFlag: item.visibleFlag,
                });
                formDetailList.value[res.formId][el.prop + index] = getVal(
                  value,
                  el.valueProp
                );
              });
            }
            if (item.fieldName === 'SMO信息') {
              fixedCombinationCode.smoInfo.forEach((el) => {
                temp.list.push({
                  prop: el.prop + index,
                  label: el.label,
                  fieldId: item.fieldId,
                  visibleFlag: item.visibleFlag,
                });
                formDetailList.value[res.formId][el.prop + index] = getVal(
                  value,
                  el.valueProp
                );
              });
            }
          });

          return;
        } else {
          temp.list.push({
            label: item.fieldName,
            prop: item.fieldId,
            fieldId: item.fieldId,
            visibleFlag: item.visibleFlag,
          });
        }
        if ([5, 6, 13].includes(item.fieldType)) {
          const optionValue =
            getVal(item, feildTypeValue[item.fieldType]) || [];
          const arr = optionValue.map(
            (item: any) => item.fullLabel || item.label
          );
          formDetailList.value[res.formId][item.fieldId] = arr.join(',');
        } else if (item.fieldType === 9) {
          fileFeildList.value.push(item.fieldId);
          const fileList = getVal(item, feildTypeValue[item.fieldType]);

          formDetailList.value[res.formId][item.fieldId] = (fileList ?? []).map(
            (el: any) => ({
              name: el.fileName,
              uid: el.fileId,
            })
          );
        } else {
          formDetailList.value[res.formId][item.fieldId] = getVal(
            item,
            feildTypeValue[item.fieldType]
          );
        }
        // visiableFlag:修改为后端处理，前端处理逻辑删除
        // if (
        //   (item.fieldType === 6 && item.optionSource === 1) ||
        //   item.fieldType === 4
        // ) {
        //   let _arr;

        //   if (item.fieldType === 4) {
        //     _arr = item.valueContextObj.optionVal
        //       ? [item.valueContextObj.optionVal.value]
        //       : [];
        //   } else {
        //     _arr = item.valueContextObj.optionsVal
        //       ? item.valueContextObj.optionsVal.map((i: any) => i.value)
        //       : [];
        //   }
        //   _arr.forEach((val: any) => {
        //     item.linkContent &&
        //       item.linkContent.forEach((el: any) => {
        //         if (el.optionVal === val) {
        //           el.linkedFields.forEach((field: any) => {
        //             const _item = formDetailList.value[
        //               res.formId
        //             ].fieldList.find(
        //               (e: any) => e.frontId === field.linkFrontId
        //             );
        //             if (_item) {
        //               _item.visibleFlag = field.visibleFlag;
        //             }
        //           });
        //         }
        //       });
        //   });
        // }
      });
      // 处理 visibleFlag
      // formDetailList.value[res.formId].fieldList.forEach((item: any) => {
      //   if (item.visibleFlag === 0) {
      //     columns.value[res.formId].forEach((_columns: any) => {
      //       if (_columns.fieldId === item.fieldId) {
      //         _columns.visibleFlag = item.visibleFlag;
      //       } else {
      //         _columns.list.forEach((child: any) => {
      //           if (child.fieldId === item.fieldId) {
      //             child.visibleFlag = item.visibleFlag;
      //           }
      //         });
      //       }
      //     });
      //   }
      // });
      previewVisible.value = true;
      resolve({
        formDetailList,
        columns,
        fileFeildList,
        previewVisible,
      });
    });
  });
};
